package com.test.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.test.domain.Department;

/**
 * 工具类<br>
 * <ul>
 * 	<li>1.实现树状结构方法</li>
 * </ul>
 * @author Administrator
 *
 */
public class DepartmentUtils {
	/**
	 * 获取指定顶点部门的所有子部门，并修改名称，实现树状结构
	 * @param topList
	 * @param removedDepartment
	 * @return
	 */
	public static List<Department> getAllDepartments(List<Department> topList,Department removedDepartment) {
		// TODO Auto-generated method stub
		List<Department> departmentList = new ArrayList<Department>();
		viewTree(topList, "┝", departmentList,removedDepartment);
		return departmentList;
	}
	/**
	 * 通过递归遍历顶点实现，遍历一颗树。在遍历的时候，将部门名称修改为┝+名称实现树状结构
	 * @param topList
	 * @param perfix
	 * @param departmentList
	 * @param departmentList:需要移除的部门，因为在修改的时候，在部门列表中不应该显示自己及自己的子孙部门
	 */
	
	private static void viewTree(Collection<Department> topList,String perfix,List<Department> departmentList,Department removeDepartment) {
		for(Department top : topList){
			//如果有要移除的部门，当循环到的当前部门的id和需要移除的部门id相等的时候，就跳出这次循环。也就不会遍历子孙部门
			if(removeDepartment!=null && top.getId().equals(removeDepartment.getId())){
				continue;
			}
			//不要修改session缓存中的对象。
			Department copy = new Department();
			copy.setId(top.getId());
			copy.setName(perfix+top.getName());
			departmentList.add(copy);
			viewTree(top.getChildren(),"　"+perfix,departmentList,removeDepartment);
		}
	}
}
